package com.weiliang.www;

import java.util.Scanner;

/**
 * @Author: liangWeiQUQ
 * @Description: 13
 * @DateTime: 2021/5/21 16:57
 **/
public class RomanToInt13 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要进行转换的罗马数字");
        String s = scanner.nextLine();
        int i = romanToInt(s);
        System.out.println("转换后的数字是" + i);
    }

    private static int romanToInt(String s) {
        int n = s.length();
        int roman = 0;
        for (int i = 0; i < n; i++) {
            switch (s.charAt(i)) {
                case 'I' : roman = roman + 1;break;
                case 'V' : roman = roman + 5;break;
                case 'X' : roman = roman + 10;break;
                case 'L' : roman = roman + 50;break;
                case 'C' : roman = roman + 100;break;
                case 'D' : roman = roman + 500;break;
                case 'M' : roman = roman + 1000;break;
                default: System.out.println("default");break;
            }
            if(i!=0)
            {
                if(((s.charAt(i)=='V')||(s.charAt(i)=='X'))&&(s.charAt(i-1)=='I'))
                {roman = roman-1*2;}
                if(((s.charAt(i)=='L')||(s.charAt(i)=='C'))&&(s.charAt(i-1)=='X'))
                {roman = roman-10*2;}
                if(((s.charAt(i)=='D')||(s.charAt(i)=='M'))&&(s.charAt(i-1)=='C'))
                { roman = roman-100*2;}
            }
        }
        return roman;
    }
}
